﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace nkl
{

    /// <summary>
    /// 以id驱动，映射，懂得自然懂，不懂爬
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BasePool<T> where T : class, new()
    {
        public List<T> dataItems;

         

        protected Dictionary<int, T> dataDict
        {
            get
            {
                if (dataItems.Count <= 0)
                {
                    return new Dictionary<int, T>();
                }
                else
                {
                    Dictionary<int, T> newDict = new Dictionary<int, T>();

                    foreach (T item in dataItems)
                    {
                        Type type = item.GetType();
                        PropertyInfo pidinfo = type.GetProperty("id");
                        int pid = (int)pidinfo.GetValue(item, null);
                        newDict.Add(pid, item);
                    }
                    return newDict;
                }
            }
        }


        public T getItem(int id)
        {
            T item;
            if (this.dataDict.TryGetValue(id, out item))
            {
                return item;
            }
            else
            {
                return null;
            }

        }
    }
}